package com.example.service;

import org.springframework.stereotype.Service;

import com.example.constant.ErrorMessageConstant;
import com.example.constant.MessageTypeConstant;
import com.example.constant.SendReasonConstant;
import com.example.entity.ChargingPile;
import com.example.entity.body.impl.ASDUBody;
import com.example.entity.factory.Unpooled;
import com.example.entity.message.Message;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;

/**
 * 处理接收到的总召报文和数据变化通知报文
 * 
 * @author yao
 *
 */
@Service
public class CallHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

		Message message = (Message) msg;

		ASDUBody body = message.getBody();
		Byte type = body.getType();
		char reason = body.getSendReason();
		ChargingPile state = (ChargingPile) ctx.channel().attr(AttributeKey.valueOf("state")).get();
		// 判断是否为总召开始或结束报文
		if (type == MessageTypeConstant.CALLED_MESSAGE) {
			// 接收到总召开始确认和已经发送了U帧
			// 或接收到总召结束报文且已经进行过总召
			System.out.println("调用了 CallHandler");
			if (reason == SendReasonConstant.AFFIRM) {
				if (state == null) {
					System.out.println("异常 state为空");
				}
				if (state.isU()) {
					System.out.println("call affirm");
					state.hasCallAffirm();
				} else {
					// 错误处理未U先总召
					ctx.fireChannelRead(Unpooled.wrapperErrorMessage(ErrorMessageConstant.NO_U));
				}

			} else if (reason == SendReasonConstant.END) {
				if (state.isCallRespond()) {
					Message message2 = Unpooled.newCountMessage();
					ctx.writeAndFlush(message2);
					state.hasCallEnd();
				} else {
					// 错误处理 未总召就结束
					ctx.fireChannelRead(Unpooled.wrapperErrorMessage(ErrorMessageConstant.NO_CALL_RESPOND));
				}

			}
		}
		// 接下来都是总召报文或数据上传报文
		else if (type == MessageTypeConstant.CALLED_RESPONED_MESSAGE01) {
			if (reason == SendReasonConstant.CALL_RESPOND) {
				if ((state.isCallAffirm() || state.isCallRespond())) {
					System.out.println("call one");
					state.hasCallRespond();
				} else {
					// 错误处理 还未开始总召
					ctx.fireChannelRead(Unpooled.wrapperErrorMessage(ErrorMessageConstant.NO_CALL_AFFIRM));
				}

			} else if (reason == SendReasonConstant.VARIATIONAL_DATUM) {
				if (state.isStart()) {

				} else {
					// TODO 错误处理 电桩还未启动完毕
				}
			} else {
				// TODO 错误处理 错误报文
			}

		} else if (type == MessageTypeConstant.CALLED_RESPONED_MESSAGE02) {
			if (reason == SendReasonConstant.CALL_RESPOND) {
				if ((state.isCallAffirm() || state.isCallRespond())) {
					System.out.println("call two");
					state.hasCallRespond();
				} else {
					// 错误处理 还未开始总召
					ctx.fireChannelRead(Unpooled.wrapperErrorMessage(ErrorMessageConstant.NO_CALL_AFFIRM));
				}

			} else if (reason == SendReasonConstant.VARIATIONAL_DATUM) {
				if (state.isStart()) {
					System.out.println("call two");
				} else {
					// TODO 错误处理 电桩还未启动完毕
				}

			}

		} else if (type == MessageTypeConstant.CALLED_RESPONED_MESSAGE03) {
			if (reason == SendReasonConstant.CALL_RESPOND) {
				if ((state.isCallAffirm() || state.isCallRespond())) {
					System.out.println("call three");
					state.hasCallRespond();
				} else {
					// 错误处理 还未开始总召
					ctx.fireChannelRead(Unpooled.wrapperErrorMessage(ErrorMessageConstant.NO_CALL_AFFIRM));
				}

			} else if (reason == SendReasonConstant.VARIATIONAL_DATUM) {
				if (state.isStart()) {
					System.out.println("call three");
				} else {
					// TODO 错误处理 电桩还未启动完毕
				}

			}

		} else {
			ctx.fireChannelRead(msg);
		}

	}

}
